package com.csdj.file;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FileCharTest {
    public static void main(String[] args) {
//        reader();
//        bufReader();
//        bufWriter();
        dataTest();
    }

    static class Inner{
        private int age;
        public void print(){
            System.out.println(this.age);
        }
    }








    /**
     * 用字符流读取文件
     */
    public static void reader(){
        //创建FileReader对象
        try {
            FileReader reader = new FileReader("E:/test/test.txt");
            StringBuilder sb = new StringBuilder();
            char[] buf = new char[256];
            while((reader.read(buf)) != -1){
                sb.append(buf);
            }
            System.out.println(sb.toString());
            reader.close();//关闭流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void bufReader(){
        try {
            FileReader reader = new FileReader("e:/test/test.txt");
            BufferedReader bufReader = new BufferedReader(reader);
            String line = bufReader.readLine();
            while(line != null){
                System.out.println(line);
                line = bufReader.readLine();
            }
            bufReader.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void bufWriter(){
        try {
            FileWriter writer = new FileWriter("e:/test/abc.txt");
            //第二参数指定缓存区的大小
            BufferedWriter bufWriter = new BufferedWriter(writer,256);
            bufWriter.write("我是一个中国人");
            bufWriter.newLine();
            bufWriter.write("i am a chinese!");

            bufWriter.flush();
            bufWriter.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void dataTest(){
        try {
            long start = System.currentTimeMillis();
            FileInputStream inputStream = new FileInputStream("e:/test/picture.png");
            DataInputStream dataIn = new DataInputStream(inputStream);
            FileOutputStream outputStream = new FileOutputStream("e:/test/target.png");
            DataOutputStream dataOut = new DataOutputStream(outputStream);

            //jdk9新增的
            dataIn.transferTo(outputStream);


            byte[] buf = new byte[1024];
            while((dataIn.read(buf)) != -1){
                dataOut.write(buf);
            }
            dataOut.flush();//清除缓存
            dataOut.close();
            outputStream.close();
            dataIn.close();

            inputStream.close();
            System.out.println(System.currentTimeMillis()-start);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
